/**
 * Created by cxj on 2016/2/22.
 */
var grid;

var showgrid;

var bootbox_global;




//同意对象
function acceptObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);

    var alertarray = {
        form: {api: "/worker/AcceptRequest", name: "alertform", id: "alertform"},
        inputs: [
            {title: "原因", type: "text", placeholder: "", verify: "", name: "reason", id: "reason"}
        ]

    }
    bootbox.dialog({
        title: "要同意该请求",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&request_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });





}

//拒绝对象
function denyObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);

    var alertarray = {
        form: {api: "/worker/denyRequest", name: "alertform", id: "alertform"},
        inputs: [
            {title: "原因", type: "text", placeholder: "", verify: "empty", name: "reason", id: "reason"}
        ]

    }
    bootbox.dialog({
        title: "拒绝该请求",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&request_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });





}

//查看对象
function viewObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];


    //发送请求
    send_request_direct("/worker/getRequestLogList", "request_id="+objid, function process_data(data) {


      //  alert(data.data.data.length);

        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+(parseInt(datarow)+1)+"</span>";
            }},
            { title: '提交时间', dataIndex: 'create_time',},
            {title : '认证状态',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var is_verify = data[datarow].status;

                if(is_verify == 0)
                    return "<span class='label-default'>未提交</span>";
                if(is_verify == 1)
                    return "<span onclick=\"bootbox_content_alert('"+data[datarow].reason+"')\" class='label-success'>认证成功<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";
                if(is_verify == 2)
                    return "<span class='label-info'>认证中</span>";
                if(is_verify == 3)
                    return "<span  onclick=\"bootbox_content_alert('"+data[datarow].reason+"')\" class='label-danger'>认证失败<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";
            }},
            { title: '原因', dataIndex: 'reason',},
            { title: '操作人', dataIndex: 'created_by',},


        ];

        var message = getGridInfo(data.data.data,columns);

        bootbox_content_alert(message);


    }, function process_fail(data) {

        //操作失败
        AlertError(bootbox, data.msg);


    });

}




define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        bootbox_global = bootbox;
        //每列数据列性
        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
            }},
            { title: '请求id', dataIndex: 'request_id',},
            { title: '手机号', dataIndex: 'mobile',},
            { title: '工人id', dataIndex: 'worker_id',},
            //{ title: '手机号', dataIndex: 'mobile',},
            {title : '认证状态',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var is_verify = data[datarow].status;

                if(is_verify == 0)
                    return "<span class='label-default'>未提交</span>";
                if(is_verify == 1)
                    return "<span onclick=\"bootbox_content_alert('"+data[datarow].reason+"')\" class='label-success'>认证成功<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";
                if(is_verify == 2)
                    return "<span class='label-info'>认证中</span>";
                if(is_verify == 3)
                    return "<span  onclick=\"bootbox_content_alert('"+data[datarow].reason+"')\" class='label-danger'>认证失败<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";




            }},
            { title: '姓名', dataIndex: 'real_name',},
            { title: '头像', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var avatar = data[datarow].avatar;

                if(avatar.length == 0)
                    return "";
                else
                   // return "<img width='60px' src='"+avatar+"'/>"
                return "<img class='girdimg' onclick=\"viewUrl('"+avatar+"')\" src='"+avatar+"'/>"


            }},

            { title: '手持照', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var avatar = data[datarow].pic_idcard;

                if(avatar.length == 0)
                    return "";
                else
                // return "<img width='60px' src='"+avatar+"'/>"
                    return "<img class='girdimg' onclick=\"viewUrl('"+avatar+"')\" src='"+avatar+"'/>"


            }},

            { title: '工种', dataIndex: 'type_name',},
            { title: '工龄(年)', dataIndex: 'work_year',},
            { title: '单价', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var price = data[datarow].price;

                return price+"元/m<sup>2</sup>"



            }},

            { title: '身份证', dataIndex: 'id_card',},
            { title: '提交时间', dataIndex: 'create_time',},
            { title: '最后更新时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var is_verify = data[datarow].status;
                var  html =  "<div class='div_btn'><button onclick=\"acceptObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-info'>同意</button> <button onclick=\"denyObj("+data[datarow].request_id+")\"  class='btn btn-danger'>拒绝</button>";






                if(is_verify == 2)
                {
                    html =  html+"<div class='div_btn'><button onclick=\"viewObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-success'>操作日志</button>";


                }
                else
                {
                    html = "<div class=''><button onclick=\"viewObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-success'>操作日志</button>";

                }

                //log


                html = html+"</div>"

                return html;
            }},




        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/worker/getworkerrequestlist", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="mobile="+$("#mobile").val()+"&status="+$("#status").val();


            return param;

        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });
    });


});